package com.grf;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class producer {

    private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;//默认的用户名
    private static final String PASSPWRD = ActiveMQConnection.DEFAULT_PASSWORD;//默认的连接密码
    private static final String BROKEURL = ActiveMQConnection.DEFAULT_BROKER_URL;//默认的连接地址


    public static void main(String[] args) {

        ConnectionFactory connectionFactory;//连接工厂
        Connection connection = null;//连接
        Session session = null;//会话
        Destination destination;//消息目的地
        MessageProducer messageProducer;//消息生产者


        try {
            //1、连接MQ
            connectionFactory = new ActiveMQConnectionFactory(USERNAME,PASSPWRD,BROKEURL);
            connection =  connectionFactory.createConnection();
            session = connection.createSession(true,Session.AUTO_ACKNOWLEDGE);
            destination =  session.createTopic("短信发送T");


            messageProducer = session.createProducer(destination);
            //2、发送消息
            for (int i = 0; i < 10; i++) {

            String txt = "257377:6126"+1;

            TextMessage message= session.createTextMessage(txt);
            messageProducer.send(destination,message);

            }
            session.commit();


        } catch (JMSException e) {
            e.printStackTrace();
        }finally {
            //3、断开
            try {
                session.close();
                connection.close();
            } catch (JMSException e) {
                e.printStackTrace();
            }

        }








    }
}
